package com.changgou.goods.controller;

import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.goods.pojo.Album;
import com.changgou.goods.service.AlbumService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author BAO
 * @version V1.0
 * @Package com.changgou.goods.controller
 * @date 2020/12/7 11:56
 * @description:
 */
@RestController
@RequestMapping("/album")
public class AlbumController {

    @Autowired
    private AlbumService albumService;

    @GetMapping("/findAll")
    public Result findAll(){
        List<Album> albumList = albumService.findAll();
        return Result.success("albumList success",albumList);
    }

    @GetMapping("/find/{albumId}")
    public Result findById(@PathVariable Integer albumId){
        return Result.success("findById success",albumService.findById(albumId));
    }

    @PostMapping("/save")
    public Result save(@RequestBody Album album){
        int save = albumService.save(album);
        return save>0?Result.success("save success",null):Result.fail("save fail");
    }

    @PutMapping("/update/{albumId}")
    public Result updateById(@RequestBody Album album,
                             @PathVariable Long albumId){
        album.setId(albumId);
        int update = albumService.updateById(album);
        return update>0?Result.success("save success",null):Result.fail("save fail");
    }

    @DeleteMapping("/delete/{albumId}")
    public Result deleteById(@PathVariable Integer albumId){
        int delete = albumService.deleteById(albumId);
        return delete>0?Result.success("save success",null):Result.fail("save fail");
    }

    @GetMapping("/findCondition")
    public Result findCondition(@RequestParam Map<String ,Object> searchMap){
        List<Album> albumList = albumService.findCondition(searchMap);
        return Result.success("findCondition success",albumList);
    }

    @GetMapping("findByPage/{currentPage}/{size}")
    public Result findByPage(@PathVariable int currentPage ,
                             @PathVariable int size){
        Page<Album> page = albumService.findByPage(currentPage, size);
        return Result.success("findByPage success", new PageResult<Album>(page.getTotal(),page.getResult()));
    }

    @GetMapping("/findByPageAndCondition/{currentPage}/{size}")
    public Result findByPageAndCondition(@RequestParam Map<String, Object> searchMap,
                                         @PathVariable int currentPage,
                                         @PathVariable int size){
        Page<Album> page = albumService.findByPageAndCondition(searchMap, currentPage, size);
        return Result.success("findByPage success", new PageResult<Album>(page.getTotal(),page.getResult()));
    }
}
